/*
 * plat_91.cpp
 *
 *  Created on: Aug 22, 2014
 *      Author: killerg
 */






#include "bill_deps.h"
#include "bill.h"
#include "bill_cfg.h"

#include <list>
#include <map>
#include <string>
#include <set>

static const int AppId = 3431355;
static const char* ProductKey = "5GNsQZKN3C2XsxPl48a0PD3i";
static const char* ProductSecret = "nLVoerlIMcaKLsu2hA44AHkD0ncIaCrx";

static void send_result(
        http_server::p_request req,
        const char* msg) {

    if (strcmp(msg, "SUCCESS")) {
        char buff[100];
        time_t now = time(NULL);
        strftime(buff,100,"%Y%m%d_%H%M%S",localtime(&now));

    	UTIL_LOG(E, "%s EPI_91 msg:%s, query:%s", buff, msg, "");
    }

    req->send_reply(200, msg);
}

static std::set<std::string> s_okip;

bool init_91() {

	s_okip.insert("111.13.46.114");
	s_okip.insert("111.13.46.115");
	s_okip.insert("111.13.46.116");
	s_okip.insert("111.13.46.117");
	s_okip.insert("111.13.46.118");
	s_okip.insert("111.13.46.122");
	s_okip.insert("111.13.46.123");
	s_okip.insert("111.13.46.124");
	s_okip.insert("111.13.46.125");
	s_okip.insert("111.13.46.126");

    g_https->handle("/bill/91", [](http_server::p_request req){
    	if (g_svrmgr->db_set >= 100) {
    		send_result(req, "ERROR_FAIL");
        	return;
    	}

    	if (!s_okip.count(req->ip)) {
    		send_result(req, "ERROR_FAIL");
        	return;
    	}

#define GETARG(arg) \
    	string s##arg = req->query[#arg];	\
    	if (s##arg.empty()) {					\
    		send_result(req, "ERROR_FAIL");	\
    		return;								\
    	}

    	GETARG(amount);
    	GETARG(cardtype);
    	GETARG(orderid);
    	GETARG(result);
    	GETARG(timetamp);
    	GETARG(aid);
    	GETARG(client_secret);
    	if (sresult == "2") {
    		send_result(req, "SUCCESS");
        	return;
    	}

    	char* buff = (char*)fmt_cstr("%s%s%s%s%s%s%s",
    	    			samount.c_str(),
    	    			scardtype.c_str(),
    	    			sorderid.c_str(),
    	    			sresult.c_str(),
    	    			stimetamp.c_str(),
    	    			ProductSecret,
    	    			encode(said.c_str()));
//    	int len = strlen(buff);
//    	for (int i=0; i<len; ++i) {
//    		buff[i] = tolower(buff[i]);
//    	}
    	string md5 = Md5(buff);

    	if (md5 != sclient_secret) {
    		send_result(req, "ERROR_SIGN");
        	return;
    	}

    	string account;
        string orderId;
        double payFee = 0;
        int serverid = -1;
        string machinecode;

        std::vector<std::string> vNote;
        split(said, ",", vNote);
		if (vNote.size() != 3) {
			send_result(req, "ERROR_FAIL");
            return;
		}
		serverid = atoi(vNote[0].c_str());
		machinecode = vNote[1];

		account = fmt_cstr("91-%s", vNote[2].c_str());
        orderId = fmt_cstr("91-%s", sorderid.c_str());
        payFee = atof(samount.c_str()) * g_cfg->Rate;


        if (payFee <= 0) {
        	send_result(req, "ERROR_FAIL");
            return;
        }

        if (serverid <= 0) {
        	send_result(req, "ERROR_FAIL");
            return;
        }

        auto playerinfo = g_svrmgr->get_player_info(serverid, account);
        if (NULL == playerinfo) {
        	send_result(req, "ERROR_FAIL");
            return;
        }

        g_svrmgr->post_order(serverid, orderId, playerinfo->playerid, payFee, machinecode, [req](bool ok){
            if (ok)
            	send_result(req, "SUCCESS");
            else
            	send_result(req, "ERROR_FAIL");
        });

    });

    return true;
}
